﻿using HuChaHuRen.HeNan.XiangCheng.Common;
using HuChaHuRen.HeNan.XiangCheng.Models.Messages;
using log4net;
using Quartz;
using Quartz.Impl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HuChaHuRen.HeNan.XiangCheng.Upload
{
    public class ScheduleManager
    {
        ISchedulerFactory _sf;
        IScheduler _sched;
        private readonly ILog logger = LogManager.GetLogger("ScheduleManager");

        public ScheduleManager()
        {
            _sf = new StdSchedulerFactory();
            _sched = _sf.GetScheduler();
            _sched.Start();
        }

        public void Start()
        {
            var baseCfg = ConfigHelp.GetBaseConfig();
            string studyUploadCron = baseCfg.StudyUploadCron;
            string dicomUploadCron = baseCfg.DicomTransferCron;

            Publish($"PACSUploadJob 装配中,Cron:'{studyUploadCron}'.");
            Publish($"DicomTransferJob 装配中,Cron:'{dicomUploadCron}'.");

            IJobDetail pacsJob = JobBuilder.Create<PACSUploadJob>()
              .WithIdentity("pacsUploadJob", "group1")
              .Build();

            ITrigger pacsTrigger = TriggerBuilder.Create()
                  .WithIdentity("pacsTrigger", "group1")
                  .WithCronSchedule(studyUploadCron)
                  .Build();


            IJobDetail dicomUploadJob = JobBuilder.Create<DicomTransferJob>()
              .WithIdentity("dicomUploadJob", "group1")
              .Build();

            ITrigger dicomTrigger = TriggerBuilder.Create()
                  .WithIdentity("dicomUploadTrigger", "group1")
                  .WithCronSchedule(dicomUploadCron)
                  .Build();


            _sched.ScheduleJob(pacsJob, pacsTrigger);
            Publish($"Schedule 已调度任务 PacsUploadJob.");
            MessageHub.DefaultMessageHub.SendMessage<JobActivityMessage>(new JobActivityMessage
            {
                JobType = 0,
                Status = "就绪"
            });

            _sched.ScheduleJob(dicomUploadJob, dicomTrigger);
            Publish($"Schedule 已调度任务 DicomTransferJob.");
            MessageHub.DefaultMessageHub.SendMessage<JobActivityMessage>(new JobActivityMessage
            {
                JobType = 1,
                Status = "就绪"
            });
        }

        public void Pause()
        {
            if (_sched != null)
            {
                if (_sched.IsStarted)
                {
                    _sched.PauseAll();
                    Publish($"Schedule已暂停所有任务.");
                }
                else
                {
                    Publish($"Schedule暂停任务失败,没有正在运行的Job.");
                }
            }
        }

        public void Resume()
        {
            if (_sched != null)
            {
                if (!_sched.IsStarted)
                {
                    _sched.ResumeAll();
                    Publish($"Schedule已恢复全部任务");

                }
                else
                {
                    Publish($"Schedule恢复任务失败,没有暂停中的Job");
                }
            }
        }
    
        public void Stop()
        {
            if (_sched != null)
            {
                _sched.Shutdown();
                Publish("已手动停止Job.");
            }
        }

        private void Publish(string msg)
        {
            logger.Info(msg);
            MessageHub.DefaultMessageHub.SendMessage<string>(msg);
        }

    }
}
